package org.fly.controller.wap.supplier;

import org.fly.bean.Supplier;
import org.fly.bean.User;
import org.fly.bean.bo.SupplierBo;
import org.fly.bean.common.BusinessException;
import org.fly.contants.ErrorConstant;
import org.fly.contants.WebConstant;
import org.fly.controller.AbstractController;
import org.fly.enums.AuditStatus;
import org.fly.service.SupplierService;
import org.fly.utils.CookieUtil;
import org.fly.utils.JsonUtils;
import org.fly.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by rongzhao.zhu on 2018/2/16.
 */
@Controller
@RequestMapping("/wap/supplier/")
public class WapSupplierMainController extends AbstractController {

    @Autowired
    SupplierService supplierService;

    @RequestMapping(value = "loginOn", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String login(String account,String password,
                        HttpServletRequest request,
                        HttpServletResponse response){
        Supplier supplier = null;
        try{
            supplier =  supplierService.login(account,password);
            CookieUtil.setLoginCookie(response,supplier,"");
        }catch (BusinessException e){
            return toExceptionString(e);

        }
        return JsonUtils.encode(createDataTip(supplier));
    }
    @RequestMapping(value = "getUser", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getUser(HttpServletResponse response){
        Supplier supplier = (Supplier) UserContext.getUser();
        return JsonUtils.encode(createDataTip(supplier));
    }

    @RequestMapping(value = "registerOn", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String registerOn(SupplierBo data, HttpServletResponse response){
        Supplier customer = null;
        try{
            supplierService.register(data,data.getVerifyCode());
            customer =  supplierService.login(data.getAccount(),data.getPassword());
            CookieUtil.setLoginCookie(response,customer,"");
        }catch (Exception e){
            return toExceptionString(e);
        }
        return toJSONString(createDataTip(customer));
    }
    @RequestMapping(value = "certify", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String certify(Supplier supplier,HttpServletResponse response){
        Supplier current = (Supplier) UserContext.getUser();


        try {
            Supplier db =  supplierService.findById(current.getId());

            if(AuditStatus.pass.name().equals(db.getAuditStatus())){
                throw new BusinessException(ErrorConstant.ec_system_error,"已经审核通过!");
            }
            db.setAuditStatus(AuditStatus.init.name());
            db.setLicense(supplier.getLicense());
            current.setAuditStatus(AuditStatus.init.name());
            current.setLicense(supplier.getLicense());
            supplierService.save(db);
        } catch (BusinessException e) {
            return toExceptionString(e);
        }
        return JsonUtils.encode(createDataTip(current));
    }
}
